package com.rt.tmpt.dto;

import com.rt.tmpt.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;

import java.io.Serializable;

/**
 * Created by wxy on 2017/9/12.
 */
@ApiModel(description = "分页传输对象")
public class PageDto implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "指定排序列名[id]")
    @ApiParam(defaultValue = "id")
    private String sortColumn = "id";

    @ApiModelProperty(value = "desc[asc]")
    @ApiParam(defaultValue = "desc")
    private String sortOrder;

    @ApiModelProperty(value = "当前页码")
    @ApiParam(defaultValue = "0")
    private int pageCurrent=0;

    @ApiModelProperty(value = "每页记录数")
    @ApiParam(defaultValue = "0")
    private int pageSize=0;

    @ApiModelProperty(value = "pageIndex=pageCurrent*pageSize")
    @ApiParam(defaultValue = "0")
    private int pageIndex=0;

    //Vue element admin
    private int page;
    private int limit;
    private String sort; // +id, -id

    /**
     * action中调用预处理，设置默认值
     * 分页/排序查询条件
     * 默认主键desc
     */
    public void configDefaultValue(){
        if(StringUtils.isNull(getSortColumn())){
            setSortColumn("id");
            setSortOrder("desc");
        }
        setPageCurrent(getPageCurrent()-1);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.pageCurrent = page;
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.pageSize =limit;
        this.limit = limit;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        if(!StringUtils.isNull(sort)){
            char c = sort.charAt(0);
            if(c=='+'){
                setSortOrder("asc");
            }else{
                setSortOrder("desc");
            }
            setSortColumn(sort.substring(1));
        }
        this.sort = sort;
    }

    public String getSortColumn() {
        return sortColumn;
    }

    public void setSortColumn(String sortColumn) {
        this.sortColumn = sortColumn;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public int getPageCurrent() {
        return pageCurrent;
    }

    public void setPageCurrent(int pageCurrent) {
        this.pageCurrent = pageCurrent;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageIndex() {
        return pageIndex=pageCurrent*pageSize;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }
}
